package com.rambo.xpd.base.demo;

import com.rambo.xpd.base.vo.StudentGridlb;

import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.events.XMLEvent;

import java.io.FileInputStream;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Create by Rambo on 2017/5/11
 **/
public class STAXDemo {

    //基于光标的方式解析XML
    private static void cursorParseXML() throws Exception {
        InputStream stream = new FileInputStream(Thread.currentThread().getContextClassLoader().getResource("demo.xml").getPath());
        XMLInputFactory factory = XMLInputFactory.newInstance();
        XMLStreamReader parser = factory.createXMLStreamReader(stream);
        List<StudentGridlb> studentGridlbList = new ArrayList<>();
        StudentGridlb studentGridlb =  null;
        while (parser.hasNext()) {
            int event = parser.next();
            if (event == XMLStreamConstants.START_DOCUMENT) {
                System.out.println("stax 解析xml 开始.....");
            }
            if (event == XMLStreamConstants.START_ELEMENT) {
                if (parser.getLocalName().equals("stu_id")) {
                    studentGridlb = new StudentGridlb();
                    studentGridlb.setStu_id(parser.getElementText());
                } else if (parser.getLocalName().equals("stu_name")) {
                    studentGridlb.setStu_name(parser.getElementText());
                } else if (parser.getLocalName().equals("stu_age")) {
                    studentGridlb.setStu_age(Integer.parseInt(parser.getElementText()));
                } else if (parser.getLocalName().equals("stu_birthday")) {
                    DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                    studentGridlb.setStu_birthday(format.parse(parser.getElementText()));
                }
            }
            if (event == XMLStreamConstants.END_ELEMENT) {
                if (parser.getLocalName().equals("studentGridlb")) {
                    studentGridlbList.add(studentGridlb);
                }
            }
            if (event == XMLStreamConstants.END_DOCUMENT) {
                System.out.println("stax 解析xml 结束.....");
            }
        }
        parser.close();
        System.out.println(studentGridlbList.toString());
    }

    //迭代方式
    private static void iteratorParseXML() throws Exception {
        XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
        XMLEventReader xmlEventReader = xmlInputFactory.createXMLEventReader(Thread.currentThread().getContextClassLoader().getResourceAsStream("demo.xml"));

        List<StudentGridlb> studentGridlbList = new ArrayList<>();
        StudentGridlb studentGridlb = null;

        while (xmlEventReader.hasNext()) {
            XMLEvent event = xmlEventReader.nextEvent();
            if (event.isStartElement()) {
                String name = event.asStartElement().getName().toString();
                if (name.equals("stu_id")) {
                    studentGridlb = new StudentGridlb();
                    studentGridlb.setStu_id(xmlEventReader.getElementText());
                } else if (name.equals("stu_name")) {
                    studentGridlb.setStu_name(xmlEventReader.getElementText());
                } else if (name.equals("stu_age")) {
                    studentGridlb.setStu_age(Integer.parseInt(xmlEventReader.getElementText()));
                } else if (name.equals("stu_birthday")) {
                    DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                    studentGridlb.setStu_birthday(format.parse(xmlEventReader.getElementText()));
                }
            }

            if (event.isEndElement()) {
                String name = event.asEndElement().getName().toString();
                if (name.equals("studentGridlb")) {
                    studentGridlbList.add(studentGridlb);
                }
            }
        }
        xmlEventReader.close();
        System.out.println(studentGridlbList.toString());
    }

    public static void main(String[] args) throws Exception {
        cursorParseXML();
        iteratorParseXML();
    }
}